package com.tensquare.article.controller;

import com.tensquare.article.pojo.Article;
import com.tensquare.article.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:zimmor
 * @Date: 2021/3/1 22:32
 * @Description 文章的控制层
 * @version: 1.0.0
 */
@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * 审核通过
     * @param articleId
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/examine/{articleId}")
    public Result examine(@PathVariable String articleId){
        articleService.updateStateById(articleId);
        return new Result(true, StatusCode.OK, "审核通过");
    }

    /**
     * 点赞
     * @param article
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/thumbup/{articleId}")
    public Result thumbup(@PathVariable(value = "articleId") String article){
        articleService.thumbup(article);
        return new Result(true, StatusCode.OK, "点赞成功");
    }
    /**
     * 分页查询
     * @param article
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/search/{page}/{size}")
    public Result findSearch(@RequestBody Article article, @PathVariable int page, @PathVariable int size) {
        Page pageData = articleService.findSearch(article, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Article>(pageData.getTotalElements(), pageData.getContent()));
    }

    /**
     * 条件查询
     * @param article
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/search")
    public Result findSearch(@RequestBody Article article) {
        return new Result(true, StatusCode.OK, "查询成功", articleService.findSearch(article));
    }

    /**
     * 获取所有文章
     * @return
     */
    @RequestMapping
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询成功", articleService.findAll());
    }
    /**
     * 根据id查询文章
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    public Result findById(@PathVariable String id) {
        return new Result(true, StatusCode.OK, "查询成功", articleService.findById(id));
    }

    /**
     * 新增
     * @param article
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Article article) {
        articleService.add(article);
        return new Result(true, StatusCode.OK, "新增成功");
    }

    /**
     * 修改
     * @param article
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/{id}")
    public Result update(@RequestBody Article article, @PathVariable String id) {
        article.setId(id);
        articleService.update(article);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public Result deleteById(@PathVariable String id) {
        articleService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }
}
